package org.example.controller;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class HttpClientTest {
    @Test
    public void testGet() throws Exception{
//        1.创建一个HttpClient对象（CloseableHttpClient）,HttpClients创建。（相当于打开浏览器）
        CloseableHttpClient httpClient= HttpClients.createDefault();
//        2.创建一个HttpGet对象，相当于一个Get请求，构造参数中设置请求的url
//        HttpGet get=new HttpGet("http://www.itcast.cn");  //不带参数的
        HttpGet get=new HttpGet("https://www.51miz.com/tupian/?utm_term=22081450&utm_source=z360&guanggao&qhclickid=3aed778631cb98af");
//        3.使用HttpClient发送请求。
        CloseableHttpResponse response= httpClient.execute(get);
//        4.得到服务端的相应。取服务端相应的html
        StatusLine statusLine= response.getStatusLine();
        System.out.println(statusLine);
        int statusCode= statusLine.getStatusCode();//状态码
        System.out.println(statusCode);
        //去服务器相应内容
        //5.打印html
        HttpEntity entity= response.getEntity();
        String html= EntityUtils.toString(entity,"UTF-8");
        System.out.println(html);
//        6.关闭流
        response.close();
        httpClient.close();
    }

    @Test
    public void testPost() throws Exception {
//        1.创建一个HttpClient对象（CloseableHttpClient）,HttpClients创建。（相当于打开浏览器）
        CloseableHttpClient httpClient= HttpClients.createDefault();
//        2.创建HttpPost
        HttpPost post=new HttpPost("http://bbs.itheima.com/search.php");
          //创建一个list,其中添加请求参数。
        List<NameValuePair> from=new ArrayList<>();
        from.add(new BasicNameValuePair("mod","forum"));
        from.add(new BasicNameValuePair("searchid","7"));
        from.add(new BasicNameValuePair("orderby","lastpost"));
        from.add(new BasicNameValuePair("ascdesc","desc"));
        from.add(new BasicNameValuePair("searchsubmit","yes"));
        from.add(new BasicNameValuePair("kw","java"));
//        3.创建一个HttpEntity对象，相当于表单
        HttpEntity httpEntity=new UrlEncodedFormEntity(from);
//        4.把表单添加到post对象中
        post.setEntity(httpEntity);
//        5.使用HttpClient发送post请求
        CloseableHttpResponse response=httpClient.execute(post);
//        6.接收服务端响应的html
        String html= EntityUtils.toString(response.getEntity(),"UTF-8");
//        7.打印html
        System.out.println(html);
//        8.关闭流
        response.close();
        httpClient.close();
    }

//使用连接池创建HttpClient
    @Test
    public void testPoolingConnectionManager()throws Exception{

//        1.创建一个连接池对象。PoolingHttpClientClientConnectionManager
        PoolingHttpClientConnectionManager cm=new PoolingHttpClientConnectionManager();
//        2.创建HttpClient时，还是HttpClients工具，使用customer(),然后设置连接池
        CloseableHttpClient httpClient=  HttpClients.custom().setConnectionManager(cm).build();
//        3.创建一个HttpGet对象
        HttpGet get=new HttpGet("http://www.itcast.cn");
//        4.发送请求接收结果
        CloseableHttpResponse response= httpClient.execute(get);
        String html = EntityUtils.toString(response.getEntity(), "utf-8");
//        5.打印结果
        System.out.println(html);
//        6.关闭Response对象
        response.close();
//                HttpClient对象不需要关闭

    }
}
